<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['params']=array();
	$return['groupset']=$this->plugin_setting('groupset');
	if($_RQ['pid']){
		$return['pid']=$return['oneid']=$_RQ['pid'];
	}else{
		$return['pid']=0;
	}
	$parentid=0;
	if($return['shopset']['level']){
		$return['islevel3']=true;
		$groupspram=array('uniacid'=>$_SESSION['uniacid'],'isshow'=>'1','mid'=>0,'parentid'=>0);
		$groupspram['onepluginname']='';
	}
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>$parentid);
	if(!$parentid){
		$groupspram['mid']=0;
		$groupspram['onepluginname']='';
	}else{
		$return['pid']=0;
	}
	$pids=pdo_getall('jy_weishop_member_qingdan',array('mid'=>$_SESSION['uid']),'pid','pid');
	if($pids){
		$pids=array_keys($pids);
	}else{
		$pids=-1;
	}
	$groupspram['id']=$pids;
	$category=pdo_getall('jy_weishop_goods_group',$groupspram,array(),'','paixu desc');
	foreach($category as &$item){
		$item['childs']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>$item['id']),array(),'','paixu desc');
	}
	$searchkeyword=$this->plugin_setting('search')['keyword'];
	$return['searchkeyword']=explode(';',$searchkeyword);
	$return['category']=$category;
	$return['pid']=0;
	set_html('title','我的清单');
	$return['sharedata']=array(
		'title'=>$return['setting']['share_title']?'分类'.$return['setting']['share_title']:'分类-在线商城',
		'desc'=>$return['setting']['share_desc']?$return['setting']['share_desc']:'在线商城，一个属于自己的个人商城！',
		'link'=>custom_url('jy_weishop/shop.groups//fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>ATTACHMENT_URL.$return['setting']['share_image']
	);
	exi($return);
}
if($_RQ['op']=='getlist'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
		'nochecked'=>0,
		'no_showsys'=>0,
		'type <>'=>4
	);
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
		$isgoods=1;
	}
	//有关键词时，分类ID也检索！
	if($_RQ['pcate']){
			$pcate=$_RQ['pcate']?intval($_RQ['pcate']):intval($_RQ['pid']);
			$params['pcate']=$pcate;
			$return['params']['pcate']=$pcate;
			$isgoods=1;
	}
	if($_RQ['ccate']){
			$ccate=$_RQ['ccate']?intval($_RQ['ccate']):intval($_RQ['cid']);
			$params['ccate']=$ccate;
			$return['params']['ccate']=$ccate;
			$isgoods=1;
	}
	if($_RQ['scate']){
			$scate=$_RQ['scate']?intval($_RQ['scate']):intval($_RQ['sid']);
			$params['scate']=$scate;
			$return['params']['scate']=$scate;
			$isgoods=1;
	}
	if($isgoods){
		$goodsids=pdo_getall('jy_weishop_goods',$params,'id','id');
		if($goodsids){
			$goodsids=array_keys($goodsids);
		}else{
			$goodsids=-1;
		}
	}
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
	);
	if($goodsids){
		$params['goodsid']=$goodsids;
	}
	//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_SESSION['os']=='windows'?12:10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$list=pdo_getslice('jy_weishop_member_qingdan',$params,array($pindex,$psize),$total,'','','id desc');
	if(empty($list)){
		exi('none');
	}else{
		$fields=array('id','title','merchid','status','total','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit');
		foreach($list as $k=>&$itemv){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$itemv['goodsid']),$fields);
			$itemv=array_merge($itemv,$goods);
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			$itemv['jianprice']=$itemv['originalprice']-$itemv['price'];
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			//按点赞数排序作品
			$pindex = max(1, 0);
			$psize = 4;
			$members=pdo_getslice('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),array($pindex,$psize),$total,array('mid'),'','id desc');
			foreach($members as &$mv){
				$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
				$mv['nickname']=$memberinfo['nickname'];
				$mv['avatar']=$memberinfo['avatar'];
			}
			$itemv['members']=$members;			
			if($itemv['merchid'] && $this->plugin_exists('jy_merch')){
				$merch=pdo_get('jy_merch_user',array('id'=>$itemv['merchid']),array('status','merchname'));
				if($merch['status']==2 && $itemv['merchid']>0){
					pdo_update('jy_weishop_goods',array('nochecked'=>1),array('merchid'=>$itemv['merchid'],'uniacid'=>$_SESSION['uniacid']));
					unset($list[$k]);
					continue;
				}
				$itemv['merchname']=$merch['merchname'];
			}
			$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id'],),'id');
			$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
			$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
			$itemv['cartid']=$cartinfo['id']?:0;
			$itemv['carttotal']=$cartinfo['total']?:0;
			$itemv['cartprice']=$cartinfo['price']?:0;
			$itemv['cartoptionid']=$cartinfo['optionid']?:0;
			$itemv['cartid']=$itemv['cartid']?:0;
			$itemv['carttotal']=$itemv['carttotal']?:0;
			$itemv['specs_list']=array();
			if($_RQ['is_specs'] && $itemv['optionid']){
				//规格
				$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				if($specs_list){
						foreach($specs_list as $k=>&$sp){
							$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
						}
				}
				//已选规格
				$selectspecs = array();
				$selespename = '';
				$specs = pdo_getcolumn('jy_weishop_goods_option',array('id'=>$itemv['optionid'],'goodsid'=>$itemv['id']),'specs');				
				$selectspecs = str_array($specs,'_');
				if($selectspecs){
						foreach($selectspecs as $k=>&$spid){
							$title=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spid,'uniacid'=>$_SESSION['uniacid']),'title');
							$selespename .=$title.'，';
						}
				}
				//默认已选参数：
				$itemv['selespename'] = $selespename;
				$itemv['specs_list'] = $specs_list;
				$itemv['specid']=$selectspecs;
				$cartnum=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$itemv['options']['id'],'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
				$itemv['post'][$itemv['options']['id']]=array('num'=>$cartnum?:0,'title'=>$selespename);
			}
			if($_SESSION['is_tomedia']){
				$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
				$itemv['video']=$itemv['video']?tomedia($itemv['video']):'';
			}
			if($_RQ['wxappjiaoyi']){
				$itemv['id']=$itemv['api_id'];
			}
		}
	}
	$list=array_values($list);
	if(empty($list)){
		exi('none');
	}else{
		exi($list);
	}
}
if($_RQ['op']=='add'){
	if(!$_SESSION['uid']){
		$return['ischecklogin']=$this->model('member')->checklogin();
	}
	$post=get_postdata();
	$goodsid=$post['goodsid'];
	$like=pdo_get('jy_weishop_member_qingdan',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$goodsid),'id');
	if($like){
			pdo_delete('jy_weishop_member_qingdan',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$goodsid));
			exi('取消成功');
	}else{
		$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodsid));
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'goodsid'=>$goodsid,
			'pid'=>$goods['pcate'],
			'cid'=>$goods['ccate'],
			'sid'=>$goods['scate'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_weishop_member_qingdan',$insert);
		exi('加入清单成功');
	}	
}
?>